<?php

namespace app\app\controller;

use app\app\model\ProjectModel;
use app\app\model\StandardProcessModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;

class StandardProcess extends BaseCurd
{
    public function initialize()
    {
        $this->model = StandardProcessModel::class;
    }

    /**
     * C&U
     * @return void
     */
    public function save()
    {
        $id = input('id', '');
        $param = $this->request->param();

        if (empty($id)) {
            $model = new $this->model();
        } else {
            $model = $this->model::where('id', $id);
        }

        $model->save($param);

        $this->success('success');
    }

    /**
     * detail
     * @param $id
     * @return void
     */
    public function detail($id = 0)
    {
        !$id && $this->error('请传入id');

        $data = $this->model::where('id', $id)->find();
        empty($data) && $this->error('查询为空');

        $this->success('success', $data);
    }

    /**
     * D
     * @return void
     * @throws DbException
     */
    public function del($id = 0)
    {
        !$id && $this->error('请传入id');

        $this->model::where('id', $id)->delete();

        $this->success('success');
    }

    /**
     * 查询项目列表
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function listGroup()
    {
        $name = input('name/s', '');

        $start_time = date('Y-01-01 00:00:00', time());
        $end_time = date('Y-12-31 23:59:59', time());
        $where[] = ['b.tc_time', '>=', $start_time];
        $where[] = ['b.kg_time', '<=', $end_time];
        if ($name) {
            $where[] = ['b.project_name|c.project_name', 'like', '%' . $name . '%'];
        }
        $data = $this->model::alias('a')
            ->leftJoin('t_project b', 'a.project_id = b.id')
            ->leftJoin('t_project c', 'b.link_project_id = c.id')
            ->where($where)
            ->group('b.id')
            ->field('b.id,b.link_project_id,b.is_parent')
            ->select();

        $project_id_array = array_column($data->toArray(), null, 'id');
        $project_id_array = array_keys($project_id_array);

        $result = [];
        foreach ($data as $v) {
            if ($v['is_parent'] == 0) {
                $result[$v['id']] = $v;
                continue;
            }

            $result[$v['link_project_id']]['children'][$v['id']]['children'][] = $v;
        }

        $project_id_array = array_merge($project_id_array, array_keys($result));

        $project = ProjectModel::where('id', 'in', $project_id_array)
            ->field('id, project_name')
            ->select();
        $project = array_column($project->toArray(), null, 'id');

        $result = $this->getProject($result, $project);

        $this->success('success', $result);
    }

    /**
     * 查询指定项目id下的列表
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function ChildrenList()
    {
        $page_size = input('pageSize', '20');

        $id = input('id', '');
        empty($id) && $this->error('请传入id');
        $where[] = ['project_id', '=', $id];

        $name = input('name', '');
        (!empty($name)) && $where[] = ['a.name', 'like', '%' . $name . '%'];

        $data = $this->model::alias('a')
            ->leftJoin('sys_file_info b', 'b.file_id = a.file_id')
            ->where($where)
            ->field('a.*, b.file_path')
            ->paginate($page_size);

        empty($data) && $this->error('未找到符合条件的资源');
        $data = $data->toArray();
        empty($data['data']) && $this->error('未找到符合条件的数据');

        $this->success('success', $data);
    }

    /**
     * 统计
     * @return void
     */
    public function stat()
    {
        // 在建工程总数
        $where = [];
        $start_time = date('Y-01-01 00:00:00', time());
        $end_time = date('Y-12-31 23:59:59', time());
        $where[] = ['tc_time', '>=', $start_time];
        $where[] = ['kg_time', '<=', $end_time];
        $where[] = ['is_parent', '=', 0];
        $project_num = ProjectModel::where('project_period', 'in', [2, 4, 1])
            ->where($where)
            ->count();

        // 完成策划数量
        $complete_project_num = $this->model::group('project_id')
            ->count();

        // 已执行
        $do_project_num = $this->model::where('status', 2)
            ->group('project_id')
            ->count();

        $this->success('success', [
            'project_num' => $project_num,
            'complete_project_num' => $complete_project_num,
            'do_project_num' => $do_project_num,
        ]);
    }

    /**
     * 详情统计
     * @return void
     */
    public function detailStat()
    {
        $id = input('id/s');
        if (!$id) $this->error('请传入id');

        $total = $this->model::where('project_id', $id)
            ->count();

        $completed = $this->model::where('project_id', $id)
            ->where('status', 2)
            ->count();

        $this->success('success', [
            'total' => $total,
            'completed' => $completed,
        ]);
    }
}